//
//  HomeMiddleMallViewController.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/8/14.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "HomeMiddleMallViewController.h"
#import "HomeMiddleMallTableViewCell.h"
#import "SimpleGoodsInfo.h"
#import "MallDetailViewController.h"

@interface HomeMiddleMallViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;
@property (strong ,nonatomic) NSMutableArray *mallArray;
@end

@implementation HomeMiddleMallViewController

-(void)viewWillAppear:(BOOL)animated
{
	[self getMallInfo];
}

- (void)viewDidLoad {
	[super viewDidLoad];
	[self buildUI];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buildUI
{
	_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 170) style:UITableViewStylePlain];
	[_tableView registerNib:[UINib nibWithNibName:@"HomeMiddleMallTableViewCell" bundle:nil] forCellReuseIdentifier:@"HomeMiddleMallCell"];
	_tableView.delegate = self;
	_tableView.dataSource = self;
	_tableView.scrollEnabled = NO;
	_tableView.separatorStyle = UITableViewCellSelectionStyleNone;
	[self.view addSubview:_tableView];
}

- (void)getMallInfo
{
	[[NetworkTool sharedTool] requestWithName:@"获取首页中间推荐信息(商城)" URLString:APIRecommendIndex parameters:nil method:@"POST" callBack:^(id responseObject) {
		
		//NSError *error = nil;
		NSArray *array = responseObject[@"data"][@"goods"];
		[self.mallArray removeAllObjects];
		for (NSDictionary *dict in array)
		{
			SimpleGoodsInfo *model = [[SimpleGoodsInfo alloc] init];
			[model setValuesForKeysWithDictionary:dict];
			[self.mallArray addObject:model];
			NSLog(@"中间商城title：%@", model);
		}
		[self.tableView reloadData];
		if (self.mallArray) {
			NSLog(@"有%ld个数据", self.mallArray.count);
			[self.tableView reloadData];
			
		}
		else
			NSLog(@"没有拿到中间商城信息");
	}];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HomeMiddleMallTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeMiddleMallCell"];
	if(!cell)
	{
		cell = [[HomeMiddleMallTableViewCell alloc] init];
	}
	[cell setModel:self.mallArray[indexPath.row]];
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	SimpleGoodsInfo *model = self.mallArray[indexPath.row];
	
	MallDetailViewController *detailVC = [[MallDetailViewController alloc] init];
	detailVC.goodId = [model.goods_id integerValue];
	detailVC.isBuytogetherMode = model.is_ecbuy;
	[self.navigationController pushViewController:detailVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 140;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 140;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return self.mallArray.count;
}

- (NSMutableArray *)mallArray
{
	if (!_mallArray) {
		_mallArray = [[NSMutableArray alloc] init];
	}
	return _mallArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
